package com.pronhub.luowowo.mgrsite.controller;

import com.pronhub.luowowo.article.domain.StrategyCatalog;
import com.pronhub.luowowo.article.domain.StrategyContent;
import com.pronhub.luowowo.article.domain.StrategyDetail;
import com.pronhub.luowowo.article.query.StrategyDetailQuery;
import com.pronhub.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.pronhub.luowowo.util.AjaxResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("strategyDetail")
public class StrategyDetailController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyService strategyService;

    @Reference
    private IStrategyThemeService strategyThemeService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

   @Reference
    private  IStrategyTagService strategyTagService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")StrategyDetailQuery qo){
        //pageInfo
        model.addAttribute("pageInfo", strategyDetailService.query(qo));
        return "strategyDetail/list";
    }

    /**
     * 回显的编写
     * @param model
     * @param id 文章的id
     * @return
     */
    @RequestMapping("/input")
    public Object input(Model model,Long id){
        //需要攻略
        //strategies 所属的攻略
        model.addAttribute("strategies",strategyService.list());
        //themes二级联动的
        model.addAttribute("themes",strategyThemeService.list());

        if(id!=null){
            StrategyDetail strategyDetail = strategyDetailService.get(id);

            StrategyContent content = strategyDetailService.getContentById(id);

            strategyDetail.setStrategyContent(content);
            model.addAttribute("strategyDetail",strategyDetail);
            //标签
            String tags = strategyTagService.selectByDetailId(id);

            model.addAttribute("tags",tags);
        }
        return "strategyDetail/input";
    }

    /**
     * 二级联动 根据所属的攻略来获得相对应的标签
     * @param strategyId
     * @return
     */
    @RequestMapping("/getCatalogByStrategyId")
    @ResponseBody
    public Object getCatalogByStrategyId(Long strategyId){
        List<StrategyCatalog> list = strategyCatalogService.getCatalogByStrategyId(strategyId);
        return list;
    }

    /**
     *  保存或者更新 主要看传过来有没有id
     * @param
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyDetail strategyDetail,String tags){
        System.err.println(233);
        strategyDetailService.saveOrUpdate(strategyDetail,tags);
        return AjaxResult.SUCCESS;
    }


}
